package org.jiamandan.com.strategy.impl.car.listener;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.YXBasicDto;
import org.jiamandan.com.strategy.impl.car.IYXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.Cp56Time2aUtil;
import org.jiamandan.com.utils.YxObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-04-04 14:16
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-04-04 14:16	         Lee        	创建
 * @end
 */
@Service(value = YxConstant.Service.CMD_59)
@AllArgsConstructor
@Slf4j
public class YxTransactionListener implements IYXContextListener {

    private static final int[] number = {32, 14, 2, 14, 14, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 8, 8, 8, 34, 2, 14, 2, 16};

    @Override
    public void doExecute(YXBasicDto dto, Channel client) throws Exception {
        log.info("接收JMD-YX结算数据(CMD = 3B)：" + dto);
        int cursor = 0, length = dto.getData().length();
        int start = 0, end = number[cursor];
        String tradeNo = CommonUtil.removeZero(StringUtils.substring(dto.getData(), start, end), true);
        start = end;
        end += number[++cursor];
        String pileNo = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String gunNo = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String startTime = Cp56Time2aUtil.hexToDateString(StringUtils.substring(dto.getData(), start, end));
        start = end;
        end += number[++cursor];
        String endTime = Cp56Time2aUtil.hexToDateString(StringUtils.substring(dto.getData(), start, end));
        /**尖时 start**/
        start = end;
        end += number[++cursor];
        String pointUnitPrice = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String pointElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String pointLoseElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String pointAmt = StringUtils.substring(dto.getData(), start, end);
        /**尖时 end**/
        /**峰时 start**/
        start = end;
        end += number[++cursor];
        String peakUnitPrice = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String peakElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String peakLoseElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String peakAmt = StringUtils.substring(dto.getData(), start, end);
        /**峰时 end**/
        /**平时 start**/
        start = end;
        end += number[++cursor];
        String flatUnitPrice = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String flatElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String flatLoseElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String flatAmt = StringUtils.substring(dto.getData(), start, end);
        /**平时 end**/
        /**谷时 start**/
        start = end;
        end += number[++cursor];
        String valleyUnitPrice = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String valleyElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String valleyLoseElectricity = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String valleyAmt = StringUtils.substring(dto.getData(), start, end);
        /**谷时 end**/
        start = end;
        end += number[++cursor];
        String voltmeterStart = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String voltmeterEnd = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String voltmeterTotal = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String voltmeterLose = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String costAmt = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String vin = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String tradeFlag = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String hexTradeTime = StringUtils.substring(dto.getData(), start, end);
        String tradeTime = Cp56Time2aUtil.hexToDateString(hexTradeTime);
        start = end;
        end += number[++cursor];
        String stopReason = StringUtils.substring(dto.getData(), start, end);
        start = end;
        end += number[++cursor];
        String physicsCardNo = StringUtils.substring(dto.getData(), start, end);
        //TODO 创建订单，
        reply(tradeNo,pileNo,client);
    }

    public static  void reply(String tradeNo,String pileNo,Channel client){
        StringBuilder builder = new StringBuilder(tradeNo);
        builder.append("00");
        String replyMsg = YxObjectUtil.pushMessage(builder.toString(), YxConstant.Cmd.CMD_40);
        log.info("交易记录确认应答(CMD = 40)：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
    }

    static String getStopReason(String profile) {
        /*
        * 0x40 结束充电，APP 远程停止
0x41 结束充电，SOC 达到 100%
0x42 结束充电，充电电量满足设定条件
0x43
结束充电，充电金额满足设定条件
0x44 结束充电，充电时间满足设定条件
0x45 结束充电，手动停止充电
0x46~0x49 其他方式（预留）
*/
        switch (profile) {
            case ("40"):
                return "90";
            case ("41"):
                return "91";
            case ("42"):
                return "92";
            case ("43"):
                return "93";
            case ("44"):
                return "94";
            case ("45"):
                return "95";
            case ("6b"):
                return "96";
            case ("6e"):
                return "60";
            default:
                return profile;
        }
    }

    public static String secondConvertHourSecond(Long second) {
        String str = "00:00:00";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        System.out.println(h);
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + ":") : "00:";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + ":";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s);
        return str;
    }
}
